package com.fuxue.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fuxue.context.BaseContext;
import com.fuxue.dto.*;
import com.fuxue.entity.Image;
import com.fuxue.entity.Question;
import com.fuxue.entity.WriteSelect;
import com.fuxue.mapper.LikeMapper;
import com.fuxue.mapper.ServerMapper;
import com.fuxue.vo.*;
import com.fuxue.websocket.WebSocketServer;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fuxue.mapper.QuestionMapper;
import com.fuxue.result.PageResult;
import com.fuxue.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ServerMapper serverMapper;
    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private WebSocketServer webSocketServer;
    @Override
    public List<QuestionAndColumVO> getSomeQuestion(Integer id) {
        List<QuestionAndColumVO>list = new ArrayList<QuestionAndColumVO>();
        list = questionMapper.getQuestionById(id);

        return list;
    }

    @Override
    public PageResult pageQuery(QuestionPageShowDTO questionPageShowDTO) {
        PageHelper.startPage(questionPageShowDTO.getPage(), questionPageShowDTO.getPageSize());

        Page<QuestionVO> page =questionMapper.pageQury(questionPageShowDTO);
        long total = page.getTotal();
        List<QuestionVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public QuestionDetialsVO getQuestionDetials(Integer id) {
        Question question = new Question();
        QuestionDetialsVO questionDetialsVO = new QuestionDetialsVO();
        question = questionMapper.getQuestionALL(id);
        BeanUtils.copyProperties(question, questionDetialsVO);
        List<String>imageList = new ArrayList<String>();
        imageList = questionMapper.getQuestionImage(id,0);
        questionDetialsVO.setImage(imageList);
        return questionDetialsVO;
    }
    /*
    查询回答完成的
     */
    @Override
    public PageResult pageFinishedQuery(QuestionPageShowDTO questionPageShowDTO) {
        PageHelper.startPage(questionPageShowDTO.getPage(), questionPageShowDTO.getPageSize());
        Page<QuestionVO> page = questionMapper.pageQury(questionPageShowDTO);
        long total = page.getTotal();
        List<QuestionVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public void setAnswer(WriteQuestionDTO writeQuestionDTO) {
        writeQuestionDTO.setCreatTime(LocalDateTime.now());
             questionMapper.setQuestion(writeQuestionDTO);
            Integer id = questionMapper.getAnswerId(writeQuestionDTO.getQd());
            System.out.println(id+"前面的是id");
            List<String>list = writeQuestionDTO.getUrl();

        if(list!=null){
            Image image;
            for (String s:list) {
                image = Image.builder()
                        .aqId(id)
                        .type(1)
                        .url(s)
                        .creatTime(LocalDateTime.now())
                        .build();
                questionMapper.setImage(image);
           }
        }

            questionMapper.addPoint(writeQuestionDTO.getServerId());
            System.out.println("修改未完成为已完成");
            questionMapper.updateFinished(writeQuestionDTO.getQd(),1);


    }
/*
写问题
 */
    @Override
    @Transactional
    public void InsertQuestion(WriteDTO writeDTO) {
             writeDTO.setCreatTime(LocalDateTime.now());
             questionMapper.insertQuestion(writeDTO);
          Integer id = questionMapper.getQId(writeDTO.getUserId());
          System.out.println("问题"+id);
        List<String>list = writeDTO.getList();
        Image image;
        System.out.println("图片"+list);
        if(list!=null){ for (String s:list) {

            image = Image.builder()
                    .aqId(id)
                    .type(0)
                    .url(s)
                    .creatTime(LocalDateTime.now())
                    .build();
            questionMapper.setImage(image);
        }}
        System.out.println("更新"+questionMapper.getIdName());
        WriteSelect writeSelects = questionMapper.getIdName();
        writeSelects.setQId(id);
        System.out.println(writeSelects);
        questionMapper.updateServerFinished(writeSelects);
    }
/*
得到所有领域
 */
    @Override
   public FiledVO GetFiled()
    {
        FiledVO filedVO=questionMapper.FindF();
        return  filedVO;
    }
/*
首页展示用户的问题
 */
@Override
   public List<QuestionUserFirsetVO> getUserQuestion(Integer id)
{
    List<QuestionUserFirsetVO>list = new ArrayList<QuestionUserFirsetVO>();
    list = questionMapper.getUserQuestionById(id);
    return list;
}

            /*
            用户模糊查询
             */
    @Override
   public List<QuestionUserFirsetVO> likeSelect(QuestonTorCDTO questonTorCDTO)
   {
       List<QuestionUserFirsetVO>list = new ArrayList<QuestionUserFirsetVO>();
       list = questionMapper.likeSelect(questonTorCDTO);
       return list;
   }

   /*
   用户分页查询已完成问题
    */
    @Override
    public PageResult pageUserFinishedQuery(UserQuestionPageShowDTO userQuestionPageShowDTO) {
        PageHelper.startPage(userQuestionPageShowDTO.getPage(), userQuestionPageShowDTO.getPageSize());
        Page<QuestionVO> page = questionMapper.UserpageQury(userQuestionPageShowDTO);
        long total = page.getTotal();
        List<QuestionVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public ALLDetialsVO getALLDetials(Integer id) {
        ALLDetialsVO allDetialsVO = new ALLDetialsVO();
        Question question = new Question();
        question = questionMapper.getQuestionALL(id);
        BeanUtils.copyProperties(question,allDetialsVO);
        //先一步到位得到相应数据
        List<String>qImage = questionMapper.getQuestionImage(id,0);
        int aId = likeMapper.GetAnswerId(id);
        System.out.println(aId);
        List<String>aImage = questionMapper.getQuestionImage(aId,1);

        AnswerDetialsVO answerDetialsVO = questionMapper.getAnswerDetials(aId);

        BeanUtils.copyProperties(answerDetialsVO,allDetialsVO);

        allDetialsVO.setQImage(qImage);
        allDetialsVO.setAImage(aImage);

        return allDetialsVO;
    }

    @Override
    @Transactional
    public void giveUp(HelpTunDTO helpTunDTO) throws JsonProcessingException {
        questionMapper.updateFinished(helpTunDTO.getId(),-1);
        Question question = questionMapper.getQuestionALL(helpTunDTO.getId());
        Long Id = BaseContext.getCurrentId();
        System.out.println(Id+"测试");
        Integer serverId = Math.toIntExact(Id);
        serverMapper.updatePoint(serverId,helpTunDTO);
    }

}
